package ModelLayer;
import java.util.ArrayList;

/**
 * Vestbjerg project
 * @GroupB2 (UCN - Computer Science DMAJ0914) 
 */
public class Purchase
{
    // instance variables
    private String id;
    private String date;
    private ArrayList<LineItem> lineItemList;
    private Customer customer;
    private Employee employee;
    private int discount;
    private String state;

    private String dispatchDate;
    private String deliveryDate;

    private static int maxDiscount = 30;

    /**
     * Constructor for objects of class Purchase
     */
    public Purchase(String id, String date)
    {
        // initialise instance variables
        this.id = id;
        this.date = date;
        lineItemList = new ArrayList<LineItem>();
        customer = null;
        discount = 0;
        state = null;
        
        dispatchDate = null;
        deliveryDate = null;    
    }

    public void setId(String id)
    {
        this.id = id;
    }

    public void setDate(String date)
    {
        this.date = date;
    }
    
    public void setDispatchDate(String dispatchDate) 
    {
        this.dispatchDate = dispatchDate;
    }
    
    public void setDeliveryDate(String deliveryDate) 
    {
        this.deliveryDate = deliveryDate;
    }

    public void setState(String state) 
    {
        this.state = state;
    }

    public void setDiscount(int newDiscount) 
    {
        if(newDiscount <= maxDiscount) {
            discount = newDiscount;
        } 
        else {
            discount = maxDiscount;
        }
    }

    public static void setMaxDiscount(int newMaxDiscount) 
    {
        maxDiscount = newMaxDiscount;
    }

    public void setCustomer(Customer newCustomer) 
    {
        customer = newCustomer;
        if(customer != null) 
        {
            setDiscount(10);
        }
    }
    
    public void setEmployee(Employee newEmployee) 
    {
        employee = newEmployee;
    }

    public String getId()
    {
        return id;
    }

    public String getDate()
    {
        return date;
    }
    
    public String getDispatchDate() 
    {
        return dispatchDate;
    }
    
    public String getDeliveryDate() 
    {
        return deliveryDate;
    }

    public String getState() 
    {
        return state;
    }

    public Customer getCustomer() 
    {
        return customer;
    }
    
    public Employee getEmployee() 
    {
        return employee;
    }

    public int getDiscount() 
    {
        return discount;
    }

    public static int getMaxDiscount() 
    {
        return maxDiscount;
    }
    
    public ArrayList<LineItem> getLineItemList() 
    {
        return lineItemList;
    }

    public void addLineItem(LineItem lineItem) 
    {
        lineItemList.add(lineItem);
    }

    public void removeLineItem(Product product) 
    {
        boolean found = false;
        int counter = 0;

        while(counter < lineItemList.size()) 
        {
            if(lineItemList.get(counter).getProduct().equals(product)) 
            {
                lineItemList.remove(lineItemList.get(counter));
            }
            else 
            {
                counter++;
            }
        }

    }

    public double subtotal() 
    {
        double subtotal = 0;
        for(LineItem lineItem : lineItemList) 
        {
            if(lineItem.getProduct().getDiscount() != 0) {
                double productPrice = (lineItem.getProduct().getPrice()) * (lineItem.getProduct().getDiscount())/100;            
                int quantity = lineItem.getQuantity();
                subtotal += (quantity * productPrice);
            }
            else {
                double productPrice = lineItem.getProduct().getPrice();            
                int quantity = lineItem.getQuantity();
                subtotal += (quantity * productPrice);
            }
        }
        if(discount != 0) {
            return subtotal - (subtotal * discount/100);
        }
        else 
        {
            return subtotal;
        }
    }

}
